package com.cg.controller.announcement;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cg.controller.AbstractController;
import com.cg.entity.AppHomePic;
import com.cg.service.impl.AppHomePicServiceImpl;
import com.cg.utils.Constant;
import com.cg.utils.DateUtils;
import com.cg.utils.Query;
import com.cg.utils.R;
import com.cg.utils.UploadUtil;
import com.cg.utils.annotation.SysLog;


/**
 * app首页轮播图配置
 * @author cumt
 *
 */
@RestController
@RequestMapping("/sys/homepic")
public class ApphomePicController extends AbstractController{
	
	@Autowired
	AppHomePicServiceImpl appHomePicServiceImpl;
	@Autowired
	private UploadUtil uploadUtil;
	/**访问地址*/
	@Value("${global.upload.path}")
	private String path;
	/**存放地址*/
	@Value("${global.upload.docBase}")
	private String docBase;
	
	private String mypath="appmenu/";
	/**
	 * 所有留言列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:homepic:list")
	public R list(@RequestParam Map<String, Object> params){
		//只有超级管理员，才能查看所有管理员列表
		if(getUserId() != Constant.SUPER_ADMIN){
			params.put("createUserId", getUserId());
		}
		
		//查询列表数据
		Query query = new Query(params);
		
//		AppHomePic homepic=new AppHomePic();
		List<AppHomePic> homepicList = appHomePicServiceImpl.queryList(query);
		int total = appHomePicServiceImpl.queryTotal(query);
		
		//PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
		
		return R.success(total, "", homepicList);
	}
	
	/**
	 * 获取评论信息
	 */
	@RequestMapping("/info")
	public R info(){
		return R.ok().put("homepic", null);
	}
	
	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:homepic:info")
	public R info(@PathVariable("id") Long id){
		AppHomePic homepic = appHomePicServiceImpl.queryObject(id);
		
		
		return R.ok().put("homepic", homepic);
	}
	
	
	/**
	 * 保存带图片
	 */
	@SysLog("保存App轮播图片")
	@RequestMapping("/save")
	@RequiresPermissions("sys:homepic:add")
	public R save(HttpServletRequest request){
//		List<String> filePath = uploadUtil.upload(request,docBase, path,"homepic/");
		
		String filePath = uploadUtil.uploadImg(request,mypath);
		AppHomePic pic = new AppHomePic();
		String url = request.getParameter("url");
		Integer code = Integer.parseInt(request.getParameter("code"));
		pic.setCode(code);
		pic.setUrl(url);
		pic.setHomepagepic(filePath);
		pic.setCreateTime(DateUtils.dateToDateString(new Date()));
		appHomePicServiceImpl.save(pic);
		return R.ok();
	}
	
	/**
	 * 保存不带图片
	 */
	@SysLog("保存App轮播图片")
	@RequestMapping("/save2")
	@RequiresPermissions("sys:homepic:add")
	public R save(@RequestBody AppHomePic appHomePic){
		appHomePic.setCreateTime(DateUtils.format(new Date()));
		appHomePicServiceImpl.save(appHomePic);
		return R.ok();
	}
	
	/**
	 * 修改带图片
	 */
	@SysLog("修改App轮播图片")
	@RequestMapping("/update")
	@RequiresPermissions("sys:homepic:update")
	public R update(HttpServletRequest request){
//		List<String> filePath = uploadUtil.upload(request,docBase, path,"homepic/");
		String filePath = uploadUtil.uploadImg(request,mypath);
		AppHomePic pic = new AppHomePic();
		String url = request.getParameter("url");
		Integer code = Integer.parseInt(request.getParameter("code"));
		pic.setCode(code);
		pic.setUrl(url);
		pic.setId(Integer.parseInt(request.getParameter("id")));
		pic.setHomepagepic(filePath);
		pic.setUpdateTime(DateUtils.format(new Date()));
		appHomePicServiceImpl.update(pic);
		return R.ok();
	}
	
	/**
	 * 修改不带图片
	 */
	@SysLog("修改App轮播图片")
	@RequestMapping("/update2")
	@RequiresPermissions("sys:homepic:update")
	public R update(@RequestBody AppHomePic homepic){
		homepic.setUpdateTime(DateUtils.format(new Date()));
		appHomePicServiceImpl.update(homepic);
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除评论")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:homepic:delete")
	public R delete(@RequestBody Long[] ids){
		
		appHomePicServiceImpl.deleteBatch(ids);
		
		return R.ok();
	}
}
